﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Configuration;
using JXT.PrimaryKey.Batman.Reflection;

namespace JXT.PrimaryKey.Batman.Core.WcfService.Configuration
{
    /// <summary>
    /// 服务错误Behavior
    /// </summary>
    public class ErrorBehaviorElement : BehaviorExtensionElement
    {
        // Fields
        private const string ELEMENT_ERROR_HANDLER_TYPE = "errorHandlerType";

        // Methods
        protected override object CreateBehavior()
        {
            Type type = null;
            try
            {
                type = this.ErrorHandlerType.GetType<IErrorHandler>();
            }
            catch (Exception exception)
            {
                throw new Exception(string.Format("Invalid errorHandlerType specified in errorHandler behavior element. {0}", exception));
            }
            return new ErrorHandlerBehavior(type);
        }

        // Properties
        public override Type BehaviorType
        {
            get
            {
                return typeof(ErrorHandlerBehavior);
            }
        }

        [ConfigurationProperty(ELEMENT_ERROR_HANDLER_TYPE, IsRequired = true)]
        public string ErrorHandlerType
        {
            get
            {
                return (string)base[ELEMENT_ERROR_HANDLER_TYPE];
            }
            set
            {
                base[ELEMENT_ERROR_HANDLER_TYPE] = value;
            }
        }
    }


}
